import redis.asyncio as redis
from web.configs.config import settings


class RedisClient:
    _instance = None

    def __init__(self):
        self.redis = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance

    async def connect(self):
        self.redis = await redis.from_url(
            f"redis://{settings.redis_host}:{settings.redis_port}/{settings.redis_db}",
            decode_responses=True
        )

    async def is_processed(self, doc_id: str) -> bool:
        return await self.redis.exists(f"pdf:{doc_id}:processed")

    async def mark_as_processed(self, doc_id: str):
        await self.redis.set(f"pdf:{doc_id}:processed", "1", ex=86400)  # 24h

    async def is_processing(self, doc_id: str) -> bool:
        return await self.redis.exists(f"pdf:{doc_id}:processing")

    async def mark_as_processing(self, doc_id: str):
        await self.redis.set(f"pdf:{doc_id}:processing", "1", ex=3600)  # 1h